//
//  TF_AlbumController.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/9.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit

class TF_AlbumController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    var tableview:UITableView!
    var datasource:[TFPlaybackAlbum] = []
    var selectedArray:[Int] = []
    @objc public var isShow:Bool = false  //记录是否显示该view
    @objc public var setAlbumBlock: ((String) -> ())?
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    @objc public func createUI() {
        self.tableview = UITableView.init(frame: CGRect.zero)
    
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = UIColor.white
        self.tableview.register(TF_AlbumCell.self, forCellReuseIdentifier: "album")
        self.tableview.rowHeight = 90
        self.tableview.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.view.addSubview(self.tableview)
        self.tableview.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
    }


    @objc public func updateAlbum(list:NSMutableArray?) {
       
        self.datasource.removeAll()
        list?.forEach({  model1 in
            self.datasource.append(model1 as! TFPlaybackAlbum)
          })
          
          for _ in 0..<self.datasource.count {
              self.selectedArray.append(0)
          }
          
          self.tableview.reloadData()

    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datasource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "album", for: indexPath) as! TF_AlbumCell
       
    
        
        cell.cellwidth = self.view.frame.size.width
        let model:TFPlaybackAlbum = self.datasource[indexPath.row]
        cell.selectedArray = self.selectedArray
        cell.setAlbumModel(model: model)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        self.datasource.forEach { album in
            album.select = false
        }
        let Model:TFPlaybackAlbum = self.datasource[indexPath.row];
        Model.select = true
        let duration :String = Model.access_token
           if self.setAlbumBlock != nil {
               self.setAlbumBlock?(duration)
           }
        
        self.tableview.reloadData()
      
    }
    


}
